package com.legal.app.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.legal.common.annotation.Log;
import com.legal.common.core.controller.BaseController;
import com.legal.common.core.domain.AjaxResult;
import com.legal.common.enums.BusinessType;
import com.legal.app.domain.Favorite;
import com.legal.app.service.IFavoriteService;
import com.legal.common.utils.poi.ExcelUtil;
import com.legal.common.core.page.TableDataInfo;
import com.legal.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.legal.app.domain.vo.FavoriteLawyerVO;
import lombok.extern.slf4j.Slf4j;

/**
 * 收藏Controller
 * 
 * @author Ale
 * @date 2025-06-21
 */
@Slf4j
@Api(tags = "用户收藏")
@RestController
@RequestMapping("/app/favorite")
public class FavoriteController extends BaseController {
    @Autowired
    private IFavoriteService favoriteService;

    /**
     * 查询收藏列表
     */
    @ApiOperation("获取收藏列表")
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermi('app:favorite:list')")
    public TableDataInfo list() {
        Long userId = SecurityUtils.getUserId();
        startPage();
        List<FavoriteLawyerVO> list = favoriteService.getFavoriteLawyerList(userId);
        return getDataTable(list);
    }

    /**
     * 导出收藏列表
     */
    @PreAuthorize("@ss.hasPermi('app:favorite:export')")
    @Log(title = "收藏", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Favorite favorite) {
        List<Favorite> list = favoriteService.selectFavoriteList(favorite);
        ExcelUtil<Favorite> util = new ExcelUtil<Favorite>(Favorite.class);
        util.exportExcel(response, list, "收藏数据");
    }

    /**
     * 获取收藏详细信息
     */
    @PreAuthorize("@ss.hasPermi('app:favorite:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(favoriteService.selectFavoriteById(id));
    }

    /**
     * 新增收藏
     */
    @ApiOperation("收藏律师")
    @PostMapping("/add")
    @PreAuthorize("@ss.hasPermi('app:favorite:add')")
    public AjaxResult add(@RequestParam Long lawyerId) {
        log.info("【收藏接口】/add - 收到请求，lawyerId: {}", lawyerId);
        return toAjax(favoriteService.addFavorite(lawyerId));
    }

    /**
     * 修改收藏
     */
    @PreAuthorize("@ss.hasPermi('app:favorite:edit')")
    @Log(title = "收藏", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Favorite favorite) {
        return toAjax(favoriteService.updateFavorite(favorite));
    }

    /**
     * 删除收藏
     */
    @PreAuthorize("@ss.hasPermi('app:favorite:remove')")
    @Log(title = "收藏", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(favoriteService.deleteFavoriteByIds(ids));
    }

    @ApiOperation("取消收藏")
    @PostMapping("/cancel")
    @PreAuthorize("@ss.hasPermi('app:favorite:cancel')")
    public AjaxResult cancel(@RequestParam Long lawyerId) {
        log.info("【收藏接口】/cancel - 收到请求，lawyerId: {}", lawyerId);
        return toAjax(favoriteService.cancelFavorite(lawyerId));
    }
}
